# coding-ut-8
'''
用户登陆页面
'''
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QCheckBox, QPushButton, QWidget
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
import sys
import json
import os
import requests


class LoginPage(QMainWindow):

    def __init__(self):

        super().__init__()

        # Label
        font = QFont('Consolas')
        font.setPixelSize(50)
        enLabel = QLabel('WHUIMS-AMS', self)
        enLabel.setFont(font)
        enLabel.setAlignment(Qt.AlignCenter)

        titleLabel = QLabel('武汉大学信息管理学院', self)
        font = QFont('heiti')
        font.setPixelSize(30)
        titleLabel.setFont(font)

        titleLabel2 = QLabel('作业管理系统', self)
        font = QFont('kaiti')
        font.setPixelSize(20)
        titleLabel2.setFont(font)
        titleLabel2.setAlignment(Qt.AlignCenter)
        titleLayout = QVBoxLayout()
        titleLayout.setAlignment(Qt.AlignHCenter)
        titleLayout.addWidget(enLabel)
        titleLayout.addWidget(titleLabel)
        titleLayout.addWidget(titleLabel2)

        userNameLabel = QLabel('用户名:', self)
        userNameLabel.setFixedWidth(40)
        # userNameLabel.setAlignment(Qt.AlignJustify)
        self.userNameLineEdit = QLineEdit(self)
        userNameLayout = QHBoxLayout()
        userNameLayout.addWidget(userNameLabel)
        userNameLayout.addWidget(self.userNameLineEdit)

        pwdLabel = QLabel('密    码:', self)
        pwdLabel.setFixedWidth(40)
        # pwdLabel.setAlignment(Qt.AlignJustify)
        self.pwdLineEdit = QLineEdit(self)
        self.pwdLineEdit.setEchoMode(QLineEdit.Password)
        pwdLayout = QHBoxLayout()
        pwdLayout.addWidget(pwdLabel)
        pwdLayout.addWidget(self.pwdLineEdit)

        self.rememberMeCheckbox = QCheckBox("记住密码", self)

        self.loginBtn = QPushButton('登陆', self)
        self.cancelBtn = QPushButton('取消', self)
        self.loginBtn.clicked.connect(self.login)
        self.cancelBtn.clicked.connect(lambda: self.close())
        loginLayout = QVBoxLayout()
        loginLayout.addStretch()
        loginLayout.addLayout(titleLayout)
        loginLayout.addSpacing(50)
        loginLayout.addLayout(userNameLayout)
        loginLayout.addLayout(pwdLayout)
        loginLayout.addWidget(self.rememberMeCheckbox)
        loginLayout.addWidget(self.loginBtn)
        loginLayout.addWidget(self.cancelBtn)
        self.loginStatusLabel = QLabel()
        loginLayout.addWidget(self.loginStatusLabel)
        loginLayout.addStretch()
        loginLayout.setContentsMargins(100, 0, 100, 0)

        widget = QWidget()
        widget.setLayout(loginLayout)
        self.setCentralWidget(widget)
        self.setGeometry(100, 100, 600, 400)

        # load savings
        self.saveFile = 'save.json'
        if os.path.exists(self.saveFile):
            settings = json.loads(open(self.saveFile).read())

            userName = settings['userName']
            pwd = settings['password']
            reme = settings['rememberMe']

            self.userNameLineEdit.setText(userName)
            self.pwdLineEdit.setText(pwd)
            self.rememberMeCheckbox.setChecked(reme)

    def login(self):
        '''登陆'''
        userName = self.userNameLineEdit.text()
        pwd = self.pwdLineEdit.text()

        rememberMe = self.rememberMeCheckbox.isChecked()
        status = self.checkUserInfo(userName, pwd)

        if status:
            if rememberMe:
                open('save.json', 'w').write(json.dumps(
                    {'userName': userName, 'password': pwd, 'rememberMe': True}))
            self.loginStatusLabel.setText('登陆成功')
        else:
            self.loginStatusLabel.setText('用户名或密码错误')

    def checkUserInfo(self, userName, password) -> bool:
        # if userName=='admin' and password=='admin':
        #     return True
        # return False
        api_url = "http://127.0.0.1:8000/login"
        data = {
            "userid": str(userName),
            "password": str(password)
        }
        response = requests.post(api_url, data=json.dumps(data))
        state = json.loads(response.text)['state']
        return state


if __name__ == "__main__":
    app = QApplication()
    page = LoginPage()
    page.show()
    sys.exit(app.exec())
